package com.sinder.campus_prophet.web.jwgl;

import cc.lyceum.api.thxy.jwgl.ThxyJwgl;
import com.sinder.campus_prophet.core.annotation.RequestLimit;
import com.sinder.campus_prophet.core.global.ResponseMessage;
import com.sinder.campus_prophet.service.jwgl.TbJwglInfoService;
import com.sinder.campus_prophet.service.jwgl.TbScheduleService;
import com.sinder.campus_prophet.service.jwgl.bean.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 课表数据接口
 * @author sinder
 * Date 2023/3/20/22:51
 **/
@RestController
@RequestMapping("schedule")
@Slf4j
public class ScheduleController {
    private final TbScheduleService scheduleService;

    private final TbJwglInfoService jwglInfoService;

    public ScheduleController(TbScheduleService scheduleService, TbJwglInfoService jwglInfoService) {
        this.scheduleService = scheduleService;
        this.jwglInfoService = jwglInfoService;
    }

    /**
     * 返回本学期周次
     * @return
     */
    @GetMapping("getWeekList")
    public ResponseMessage getWeekList() {
        return scheduleService.getWeekList();
    }
    /**
     * 本周课表查询
     * @param scheduleDto
     * @return
     */
    @PostMapping("getScheduleList")
    public ResponseMessage getScheduleList(@RequestBody @Validated ScheduleDto scheduleDto) {
        return scheduleService.getScheduleList(scheduleDto);
    }

    /**
     * 课表同步 单独同步教务数据
     * RequestLimit(count=1, time=1800000)：半小时内不能超过1次同步操作，避免重复点击，恶意请求
     * @param scheduleSynDto
     * @return
     */
    @RequestLimit(count = 3, time = 600000)
    @PostMapping("synScheduleData")
    public ResponseMessage synScheduleData(@Validated @RequestBody ScheduleSynDto scheduleSynDto, HttpServletRequest request) {
        // 先校验账号密码是否正确
        ThxyJwgl jwgl = jwglInfoService.checkJwglLogin(scheduleSynDto.getOpenId(), request);
        return scheduleService.synScheduleData(scheduleSynDto, jwgl);
    }

    /**
     * 获取今日课表
     * @param scheduleTodayDto
     * @return
     */
    @PostMapping("getTodayScheduleList")
    public ResponseMessage getTodayScheduleList(@Validated @RequestBody ScheduleTodayDto scheduleTodayDto) {
        return scheduleService.getTodayScheduleList(scheduleTodayDto);
    }

    /**
     * 添加课程(自定义)
     * @param scheduleAddDto
     * @return
     */
    @PostMapping("addScheduleByOpenId")
    public ResponseMessage addScheduleByOpenId(@Validated @RequestBody ScheduleAddDto scheduleAddDto) {
        return scheduleService.addScheduleByOpenId(scheduleAddDto);
    }

    /**
     * 移除课程(自定义)
     * @param scheduleDelDto
     * @return
     */
    @PostMapping("delScheduleById")
    public ResponseMessage delScheduleById(@Validated @RequestBody ScheduleDelDto scheduleDelDto) {
        return scheduleService.delScheduleById(scheduleDelDto);
    }

    /**
     * 恢复课程
     * @param scheduleDelDto
     * @return
     */
    @PostMapping("restoreScheduleById")
    public ResponseMessage restoreScheduleById(@Validated @RequestBody ScheduleDelDto scheduleDelDto) {
        return scheduleService.restoreScheduleById(scheduleDelDto);
    }
}
